package top.dancun.eascourse.feign.service;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;
import top.dancun.eascourse.entity.StudentEntity;
import top.dancun.eascourse.feign.StudentFeign;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class StudentFeignService {
    @Autowired
    StudentFeign studentFeign;

    /**
     * 根据学号、班级获取学生信息
     * @param ids 学号集合
     * @param cids 班级id集合
     * @return
     */
    public PageUtils getByIds(Map<String,String> params,List<String> ids, List<Integer> cids){
        R r = studentFeign.getStudentByIds(params,ids,cids);
        List<StudentEntity> studentList = new ArrayList<>();
        PageUtils roomPage = null;
        if ("20000".equals(r.get("code").toString())) {
//            远程调用返回类型为list或者map
            Map<String,Object> pageData = (Map<String, Object>) r.get("page");
            //数据转换为pageUtils
            roomPage = JSON.parseObject(JSON.toJSONString(pageData),PageUtils.class);
            //获取分页list
            List<Map<String,String>> roomList = (List<Map<String, String>>) roomPage.getList();
            roomList.forEach(map -> {
                //迭代将map改为实体类
                StudentEntity student = JSON.parseObject(
                        JSON.toJSONString(map),
                        StudentEntity.class);
                studentList.add(student);
            });
            roomPage.setList(studentList);
        }

        return roomPage;
    }

}
